// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema

const validator = {
  category_id: {
    rules: [
      { required: true },
      {
        format: "string",
      },
    ],
    title: "分类",
    label: "分类",
    description: "优惠券分类，关联活动分类",
    foreignKey: "news-articles-image._id",
  },
  user_id: {
    rules: [
      {
        required: true,
      },
      {
        format: "string",
      },
    ],
    description: "优惠券所属用户ID，参考uni-id-users表",
    foreignKey: "uni-id-users._id",
    defaultValue: {
      $env: "uid",
    },
  },
  discount: {
    rules: [
      {
        required: true,
      },
      {
        format: "int",
      },
      {
        minimum: 1,
        maximum: 99,
      },
    ],
    title: "折扣率",
    label: "折扣率",
    description: "优惠券折扣率，范围1-99，例如：90表示9折",
  },
  name: {
    rules: [
      {
        required: true,
      },
      {
        format: "string",
      },
    ],
    title: "优惠券名称",
    label: "优惠券名称",
  },
  validity: {
    rules: [
      {
        required: true,
      },
      {
        format: "timestamp", // 修改为 timestamp 类型
      },
    ],
    title: "有效期",
    label: "有效期",
  },
  status: {
    rules: [
      {
        format: "int",
      },
      {
        range: [
          {
            value: 0,
            text: "未使用",
          },
          {
            value: 1,
            text: "已使用",
          },
          {
            value: 2,
            text: "已过期",
          },
        ],
      },
    ],
    title: "优惠券状态",
    defaultValue: 0,
    label: "优惠券状态",
  },
  customImage: {
    rules: [
      {
        validateFunction: function (rule, value, data, callback) {
          // 处理对象格式的图片数据
          const url = typeof value === "object" ? value.url : value;
          if (!url) {
            callback("请上传活动简介图片");
            return;
          }
          // 验证图片URL格式
          const pattern = /^(http|https):\/\/.*\.(jpg|jpeg|png|gif|bmp|webp)$/;
          if (!pattern.test(url)) {
            callback("图片格式不正确");
            return;
          }
          callback();
        },
      },
    ],
    title: "自定义图片",
    label: "自定义图片",
  },
  create_date: {
    rules: [
      {
        format: "timestamp",
      },
    ],
    title: "创建时间",
    defaultValue: {
      $env: "now",
    },
    label: "创建时间",
  },
  last_modify_date: {
    rules: [
      {
        format: "timestamp",
      },
    ],
    title: "最后修改时间",
    defaultValue: {
      $env: "now",
    },
    label: "最后修改时间",
  },
};

const enumConverter = {
  status_valuetotext: {
    0: "未发布",
    1: "已发布",
    2: "已过期",
  },
};

function filterToWhere(filter, command) {
  let where = {};
  for (let field in filter) {
    let { type, value } = filter[field];
    switch (type) {
      case "search":
        if (typeof value === "string" && value.length) {
          where[field] = new RegExp(value);
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = [];
          for (let s of value) {
            selectValue.push(command.eq(s));
          }
          where[field] = command.or(selectValue);
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0];
          let lt = value[1];
          where[field] = command.and([command.gte(gt), command.lte(lt)]);
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value;
          let startDate = new Date(s);
          let endDate = new Date(e);
          where[field] = command.and([
            command.gte(startDate),
            command.lte(endDate),
          ]);
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value;
          where[field] = command.and([
            command.gte(startDate),
            command.lte(endDate),
          ]);
        }
        break;
    }
  }
  return where;
}

export { validator, enumConverter, filterToWhere };
